package com.tianyang.yukuaifuzs.business.trade.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.BankManageContract;
import com.tianyang.yukuaifuzs.business.trade.contract.TradeConfig;
import com.tianyang.yukuaifuzs.business.trade.presenter.BankManagePresenter;
import com.tianyang.yukuaifuzs.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.common.view.BankCardTextView;
import com.tianyang.yukuaifuzs.module.roundview.RoundRelativeLayout;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：银行卡管理
 * Created by xyj on 2024/3/10
 */
public class BankManageActivity extends BaseTitleActivity implements BankManageContract.View {

    @BindView(R.id.bankNumberTv)
    BankCardTextView bankNumberTv;
    @BindView(R.id.bankNameTv)
    TextView bankNameTv;
    @BindView(R.id.addBankRrl)
    RoundRelativeLayout addBankRrl;
    @BindView(R.id.bankInfoRrl)
    RoundRelativeLayout bankInfoRrl;
    @BindView(R.id.bottomHintLl)
    LinearLayout bottomHintLl;

    private BankManageContract.Presenter mPresenter;

    private SettleBankOutput mOutput;

    private boolean isAgent;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_bank_manage;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("银行卡管理");
        isAgent = getIntent().getBooleanExtra(TradeConfig.INTENT_IS_AGENT, false);
        mPresenter = new BankManagePresenter(this, isAgent);
        mPresenter.queryUserSettlementCard();
    }

    @OnClick({R.id.updateTv, R.id.addBankRrl})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.updateTv:
            case R.id.addBankRrl:
                TradeOpenProvider.getInstance()
                        .openUpdateSettleCardActivity(mContext, mOutput, isAgent, CommonConfig.REQUESTCODE_UPDATE_SETTLE_BANK);
                break;
        }
    }

    @Override
    public void showSettlementCardView(SettleBankOutput output, boolean isAgent) {
        this.mOutput = output;
        if (output != null) {
            if (isAgent) {
                if (!TextUtils.isEmpty(output.getBankCardId()))
                    bankNumberTv.setBankCardText(VerifyUtil.secretBankCardNumber(output.getBankCardId()));
            } else {
                if (!TextUtils.isEmpty(output.getBankCard()))
                    bankNumberTv.setBankCardText(VerifyUtil.secretBankCardNumber(output.getBankCard()));
            }
            bankNameTv.setText(VerifyUtil.checkString(output.getBankName()));
            bankInfoRrl.setVisibility(View.VISIBLE);
            addBankRrl.setVisibility(View.GONE);
            bottomHintLl.setVisibility(View.VISIBLE);
        } else {
            bankInfoRrl.setVisibility(View.GONE);
            addBankRrl.setVisibility(View.VISIBLE);
            bottomHintLl.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_UPDATE_SETTLE_BANK && resultCode == RESULT_OK) {
            mPresenter.queryUserSettlementCard();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(BankManageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
